package com.jincp.filepicker.ui.audio;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;

import com.jincp.common.dialog.BaseDialogBuilder;
import com.jincp.common.permission.MPermission;
import com.jincp.common.utils.AppUtils;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.Constant;
import com.jincp.filepicker.R;
import com.jincp.filepicker.base.BasePickerBuilder;
import com.jincp.filepicker.bean.AudioFile;
import com.jincp.filepicker.base.BasePickerManager;
import com.jincp.filepicker.bean.BaseFile;
import com.jincp.filepicker.util.MediaFileUtil;
import com.jincp.filepicker.widgets.CropImageView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * =========================================================
 * @ProjectName:
 * @ClassName: AudioPickerManager
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/23 15:54
 * =========================================================
 */
public class AudioPickerManager extends BasePickerManager<AudioPickerManager>{

    public static final String TAG = AudioPickerManager.class.getSimpleName();

    public static final int REQUEST_AUDIO_TAKE = 2001;
    public static final int RESULT_AUDIO_ITEMS = 2004;

    public static final String EXTRA_RESULT_ITEMS = "EXTRA_RESULT_ITEMS";

    private static volatile AudioPickerManager mInstance;

    private AudioPickerManager() {
    }

    public static AudioPickerManager getInstance() {
        if (mInstance == null) {
            synchronized (AudioPickerManager.class) {
                if (mInstance == null) {
                    mInstance = new AudioPickerManager();
                }
            }
        }
        return mInstance;
    }

    public AudioPickerManager reset() {
        setMultiMode(false);
        setMaxSelectedLimit(9);
        return this;
    }

    public AudioPickerManager setMaxSelectedLimit(int maxSelectedLimit) {
        this.maxSelectedLimit = maxSelectedLimit;
        return this;
    }

    public void pickAudio(Activity activity){
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(activity, Constant.PERMISSION)) {
            Intent pickAudioIntent = new Intent(activity, AudioPickActivity.class);
            activity.startActivityForResult(pickAudioIntent, AudioPickerManager.REQUEST_AUDIO_TAKE);
        } else {
            MPermission.with(activity).permissions(Constant.PERMISSION).request();
        }
    }

    public void pickAudio(Fragment fragment){
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(fragment.getActivity(), Constant.PERMISSION)) {
            Intent pickAudioIntent = new Intent(fragment.getActivity(), AudioPickActivity.class);
            fragment.startActivityForResult(pickAudioIntent, AudioPickerManager.REQUEST_AUDIO_TAKE);
        } else {
            MPermission.with(fragment).permissions(Constant.PERMISSION).request();
        }
    }

    public void playAudio(Activity activity, AudioFile audio) {
        File file = new File(audio.getPath());
        Intent playAudioIntent = new Intent(Intent.ACTION_VIEW);
        Uri uri = AppUtils.getFileUri(activity, file);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            playAudioIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            playAudioIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        playAudioIntent.setDataAndType(uri, "audio/mp3");
        if (MediaFileUtil.detectIntent(activity, playAudioIntent)) {
            activity.startActivity(playAudioIntent);
        } else {
            ToastUtil.getInstance().show(R.string.no_audio_play_app);
        }
    }
}